package com.itheima.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.pms.PmsBrandMapper;
import com.itheima.pojo.pms.PmsBrand;
import com.itheima.service.pms.PmsBrandService;
import com.itheima.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand>implements PmsBrandService {
    /**
     * 根据品牌名称分页获取品牌列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo listBrand(String keyword, Integer pageNum, Integer pageSize) {
        Page <PmsBrand>page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword),PmsBrand::getName,keyword).
        or().like(StringUtils.isNotEmpty(keyword),PmsBrand::getFirstLetter,keyword);

        wrapper.orderByDesc(PmsBrand::getSort);

        Page<PmsBrand> pmsBrandPage = this.page(page, wrapper);
        int total = (int)pmsBrandPage.getTotal();
        int current = (int) pmsBrandPage.getCurrent();

        //封装分页数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(current);
        pageInfo.setList(pmsBrandPage.getRecords());

        return pageInfo;
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     */
    @Override
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        List<PmsBrand>list=this.listByIds(ids);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setShowStatus((long)showStatus);
        }
        this.updateBatchById(list);
    }

    @Override
    public void updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        List<PmsBrand>list=this.listByIds(ids);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setFactoryStatus((long)factoryStatus);
        }
        this.updateBatchById(list);
    }


}
